package com.roc.web.action;

import java.util.ArrayList;
import java.util.List;

import com.roc.page.BaseSearchCondition;
import com.roc.page.WhereHQLFieldBean;
import com.roc.util.Constance;
import com.roc.util.constant.Role;

public class TerminalSearchCondition extends BaseSearchCondition{

	private String orgCode;
	private String tid;
	private String mid;
	private String midName;
	private String obj;
	private String roleType;
	private  String  opId;//操作员id
	private String oneOrgCode;//小机构号
	private String flag;//开通状态
	private String termAddr;//门店地址
	private String terminalSn;
	private String realName;
	private String mobile;
	private String cardNo;
	private String groupCd;
	private long agentId;
	
	@Override
	protected List<WhereHQLFieldBean> getWhereHQLFieldBeanList() {
		// TODO Auto-generated method stub
		List<WhereHQLFieldBean> filedBeanList = new ArrayList<WhereHQLFieldBean>();
		
		if (Constance.isNotEmpty(orgCode)){//收单机构号
			filedBeanList.add(new WhereHQLFieldBean("tm.bank_belong_cd",orgCode, "=", false));
		}if(Constance.isNotEmpty(oneOrgCode)&&!roleType.equals(Role.AGENT.getCode())&&!(roleType.equals(Role.SYSTEM.getCode())||roleType.equals(Role.MARKET_DIRECT.getCode())||roleType.equals(Role.MARKET_LEAD.getCode()))){//小机构
	    		filedBeanList.add(new WhereHQLFieldBean("tm.uc_bc_cd_area", oneOrgCode.trim(), "=", false));
	    }
		if (Constance.isNotEmpty(tid)) {
			filedBeanList.add(new WhereHQLFieldBean("tt.TERM_ID", tid.trim(), "=", false));
		}
		if (Constance.isNotEmpty(mid)) {
			filedBeanList.add(new WhereHQLFieldBean("tt.mcht_cd", mid.trim(), "=", false));
		}
		if (Constance.isNotEmpty(midName)) {
			filedBeanList.add(new WhereHQLFieldBean("tm.name", midName.trim(), "LIKE", false,"ALL"));
		}
		if (Constance.isNotEmpty(flag)) {
			filedBeanList.add(new WhereHQLFieldBean("tt.status", flag.trim(), "=", false));
		}
		if (Constance.isNotEmpty(terminalSn)) {
			filedBeanList.add(new WhereHQLFieldBean("tt.DEVICE_CD", terminalSn.trim(), "=", false));
		}		
		if (Constance.isNotEmpty(realName)) {
			if(roleType.equals(Role.AGENT.getCode()) || roleType.equals(Role.SFB_AGENT.getCode()))
			{
				filedBeanList.add(new WhereHQLFieldBean("ma.agentname", realName.trim(), "LIKE", false,"ALL"));
			}
			else{
				filedBeanList.add(new WhereHQLFieldBean("op.real_name", realName.trim(), "LIKE", false,"ALL"));
			}
		}
		if (Constance.isNotEmpty(mobile)) {
			if(roleType.equals(Role.AGENT.getCode()) || roleType.equals(Role.SFB_AGENT.getCode()))
			{
				filedBeanList.add(new WhereHQLFieldBean("ma.agenttel", mobile.trim(), "=", false));
			}
			else{
				filedBeanList.add(new WhereHQLFieldBean("op.mobile", mobile.trim(), "=", false));
			}			
		}
		/*if (Constance.isNotEmpty(cardNo)) {
			filedBeanList.add(new WhereHQLFieldBean("op.card_no", cardNo.trim(), "=", false));
		}*/		
		if(Constance.isNotEmpty(opId) && roleType.equals(Role.AGENT_BUSS.getCode())){//操作员 
	    	filedBeanList.add(new WhereHQLFieldBean("tm.rec_upd_opr", opId.trim(), "=", false));
	    }
		return filedBeanList;
	}

	@Override
	protected String getRowCountHQL() {
		StringBuffer sb=new StringBuffer();
		sb.append(" select count(*) from tbl_term_inf tt,tbl_mcht_inf tm, ");
		if(roleType.equals(Role.AGENT.getCode()) || roleType.equals(Role.SFB_AGENT.getCode()))
		{
			sb.append(" tbl_mer_agent_relation ma ");
		}	
		else{
			sb.append(" t_operators op ");
		}
		sb.append(" where tt.mcht_cd = tm.mcht_cd ");
		if(roleType.equals(Role.AGENT.getCode()) || roleType.equals(Role.SFB_AGENT.getCode()))
		{
			sb.append(" and ma.merno = tt.mcht_cd and ma.agentid=").append(opId);
		}
		else{
			sb.append(" and tm.ext1 = op.id ");
		}
		if(roleType.equals(Role.MARKET_DIRECT.getCode()) ||roleType.equals(Role.MARKET_LEAD.getCode()))
		{
			sb.append("  and exists (select 1  from t_oper_role orole ");
	        sb.append("    where tm.uc_bc_cd_area = orole.agentid ");
	        sb.append("      and orole.operid =").append(opId).append(")");
		}
		if(groupCd.equals("2")){
			sb.append(" and tm.group_cd='2' ");
		}
		return sb.toString();
	}

	@Override
	protected String getSearchHQL() {
		StringBuffer sb=new StringBuffer();
		sb.append(" select tt.mcht_cd,tm.name,tm.bank_belong_cd,tt.term_id,tm.name_busi,tt.installlocation,tt.term_tp,tt.status,tt.device_cd,");
		if(roleType.equals(Role.AGENT.getCode()) || roleType.equals(Role.SFB_AGENT.getCode())) 
		{
			sb.append(" ma.agentname ");
		}
		else{
			sb.append(" op.real_name ");
		}
		sb.append(" from tbl_term_inf tt,tbl_mcht_inf tm, ");
		if(roleType.equals(Role.AGENT.getCode()) || roleType.equals(Role.SFB_AGENT.getCode()))
		{
			sb.append(" tbl_mer_agent_relation ma ");
		}
		else{
			sb.append(" t_operators op ");
		}
		sb.append(" where tt.mcht_cd = tm.mcht_cd ");
		if(roleType.equals(Role.AGENT.getCode()) || roleType.equals(Role.SFB_AGENT.getCode()))
		{
			sb.append(" and ma.merno = tt.mcht_cd and ma.agentid=").append(opId);
		}
		else{
			sb.append(" and tm.ext1 = op.id ");
		}
		if(roleType.equals(Role.MARKET_DIRECT.getCode()) ||roleType.equals(Role.MARKET_LEAD.getCode()))
		{
			sb.append("  and exists (select 1  from t_oper_role orole ");
	        sb.append("    where tm.uc_bc_cd_area = orole.agentid ");
	        sb.append("      and orole.operid =").append(opId).append(")");
		}
		if(groupCd.equals("2")){
			sb.append(" and tm.group_cd='2' ");
		}
		return sb.toString();
	}

	@Override
	protected String getOrderPartOfHQL() {
		// TODO Auto-generated method stub
		return " ORDER BY tt.mcht_cd DESC ";
	}

	public String getTid() {
		return tid;
	}

	public void setTid(String tid) {
		this.tid = tid;
	}

	public String getMid() {
		return mid;
	}

	public void setMid(String mid) {
		this.mid = mid;
	}

	public String getMidName() {
		return midName;
	}

	public void setMidName(String midName) {
		this.midName = midName;
	}

	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	public String getObj() {
		return obj;
	}

	public void setObj(String obj) {
		this.obj = obj;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getTermAddr() {
		return termAddr;
	}

	public void setTermAddr(String termAddr) {
		this.termAddr = termAddr;
	}

	public String getTerminalSn() {
		return terminalSn;
	}

	public void setTerminalSn(String terminalSn) {
		this.terminalSn = terminalSn;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getCardNo() {
		return cardNo;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}

	public long getAgentId() {
		return agentId;
	}

	public void setAgentId(long agentId) {
		this.agentId = agentId;
	}

	public String getOpId() {
		return opId;
	}

	public void setOpId(String opId) {
		this.opId = opId;
	}

	public String getRoleType() {
		return roleType;
	}

	public void setRoleType(String roleType) {
		this.roleType = roleType;
	}

	public String getOneOrgCode() {
		return oneOrgCode;
	}

	public void setOneOrgCode(String oneOrgCode) {
		this.oneOrgCode = oneOrgCode;
	}

	public String getGroupCd() {
		return groupCd;
	}

	public void setGroupCd(String groupCd) {
		this.groupCd = groupCd;
	}
	
}
